package com.east.controller.fee;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.east.bean.Fee;
import com.east.bean.Project;
import com.east.controller.fee.info.Subject;
import com.east.service.fee.FeeService;
import com.east.utils.FileUtils;
import com.east.utils.PageJson;
import com.east.utils.Pagenation;
import com.east.utils.Result;
import com.east.utils.ResultUtil;
import com.east.utils.StringUtils;
import com.east.utils.UploadUtil;
import com.github.pagehelper.Page;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.util.PoiPublicUtil;

/**
 * 项目费用
 * @author Administrator
 *
 */
@RestController
@RequestMapping("fee")
public class FeeController {
		@Autowired
		FeeService feeService;
		/**
		 * 全查
		 * 分页
		 * @param pagenation
		 * @param key
		 * @return
		 */
		@GetMapping("all")
		@RequiresPermissions("fee:all")
		public Result findByPage(Pagenation pagenation,String key,String subject ,String startTime ,String endTime) {
			Page<Fee> findByPage = feeService.findByPage(pagenation, key,subject,startTime,endTime);
			Object pageJson = new PageJson<Fee>().pageJson(findByPage);
			return ResultUtil.success(pageJson);
		}
		/**
		 * 详情
		 * @param fee
		 * @return
		 */
		@GetMapping("findOne")
		@RequiresPermissions("fee:findOne")
		public Result findByfindOne(Fee fee) {
			Fee findByProjectId = feeService.findByOne(fee);
			return ResultUtil.success(findByProjectId);
		}
		/**
		 * 添加
		 * @param fee
		 * @return
		 */
		@PostMapping("add")
		@RequiresPermissions("fee:add")
		public Result findByAdd(Fee fee) {
			  boolean saveProject = feeService.saveFee(fee);
			  if(saveProject) {
				  return ResultUtil.success("保存成功");
			  }else {
				  return ResultUtil.success("保存失败");
			  }
			
		}
		/**
		 * 修改
		 * @param fee
		 * @return
		 */
		@PostMapping("update")
		@RequiresPermissions("fee:update")
		public Result findByUpdate(Fee fee) {
			System.out.println(JSON.toJSON(fee));
			  boolean updateProject = feeService.updateFee(fee);
			  if(updateProject) {
				  return ResultUtil.success("修改成功");
			  }else {
				  return ResultUtil.success("修改失败");
			  }
		}
		/**
		 * 删除
		 * @param fee
		 * @return
		 */
		@DeleteMapping("del")
		@RequiresPermissions("fee:del")
		public Result findByDel(Fee fee) {
			 boolean findByProjectId = feeService.delFee(fee);
			 if(findByProjectId) {
				  return ResultUtil.success("删除成功");
			 }else {
					  return ResultUtil.success("删除失败");
			 }
		}
		
		/**
		 * 导入
		 * @param fee
		 * @return
		 * @throws Exception 
		 */
		@PostMapping("import")
		@RequiresPermissions("fee:import")
		public Result findByimport(MultipartFile file,HttpServletRequest request, HttpServletResponse response) throws Exception {
			String random = StringUtils.random();
			UploadUtil.createDir("E:\\\\mis/upload/fee/"+random);
			file.transferTo(new File("E:\\\\mis/upload/fee/"+random+"/"+file.getOriginalFilename()));
			List<Fee> list = FileUtils.importExcel(
					"E:\\\\mis/upload/fee/"+random+"/"+file.getOriginalFilename(),
			           0,1,Fee.class);	
			String saveListFee = feeService.saveListFee(list);
			return ResultUtil.success(saveListFee);
		}
		/**
		 * 科目汇总
		 * @param fee
		 * @return
		 */
		@GetMapping("subject")
		@RequiresPermissions("fee:subject")
		public Result findBysubject(String year,String month) {
			 List<Subject> fingBySubject = feeService.fingBySubject(year, month);
			 
			return ResultUtil.success(fingBySubject);
		}
		/**
		 * 项目汇总
		 * @param fee
		 * @return
		 */
		@GetMapping("project")
		@RequiresPermissions("fee:project")
		public Result findBySummary(String key,Pagenation pagenation) {
			Page<Subject> findByProject2 = feeService.findByProject(key,pagenation);
			 Object pageJson = new PageJson<Subject>().pageJson(findByProject2);
			return ResultUtil.success(pageJson);
		}
}
